/*
	File:	main.h
	Author: TJ Ashby

	Comments:
		The entry point into the game,it initializes everything and shuts them down
*/
#include "Muzzy A Wrappers/D3DManager.h"
#include "Muzzy A Wrappers/DirectInput.h"
#include "Muzzy A Wrappers/TextureManager.h"
#include "Muzzy A Wrappers/SoundManager.h"
#include "Muzzy A Wrappers/SoundManager.h"
#include "Muzzy A Source/Game.h"

// Memory Leak Detection
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <vld.h>
#endif

#define MINIMIZED		2
#define MAXIMIZED		3
#define DEFAULT			5


// Window Resolution
#define WINDOW_WIDTH	1680
#define WINDOW_HEIGHT	1050
#define FULLSCREEN		false


void PrintDebugInfo(const char *szString);
LRESULT CALLBACK WinProc(HWND hwnd,unsigned msg,WPARAM wParam,LPARAM lParam);

void Initialize(HINSTANCE);
void Shutdown();

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *szCmdLine,int nCmdShow)
{
	Initialize(hInst);

	PrintDebugInfo( "\nRunning...\n" );
	PrintDebugInfo( "----------\n" );

	// Run the game
	Game::getInstance()->Run();

	PrintDebugInfo("Closing the window...\n");
	DestroyWindow(D3DManager::getInstance()->getHWND());

	PrintDebugInfo("\nReturn Success!\n\n");
	return 0;
}

// Console "main" calls Windows "WinMain":
#ifdef _DEBUG
int main(void)
{
	WinMain(GetModuleHandle(NULL), NULL,
		nullptr, SW_SHOWDEFAULT);

	//system("PAUSE");
	return 0;
}
#endif

LRESULT CALLBACK WinProc(HWND hwnd,unsigned msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_QUIT:
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		Shutdown();
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hwnd,msg,wParam,lParam);
	}

	return 0;
}

void Initialize(HINSTANCE hInst)
{
#ifdef _DEBUG
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
	_CrtSetBreakAlloc(-1);
#endif

	PrintDebugInfo("\n\nMuzzy A System Debugger\n\n");
	PrintDebugInfo("Initialize\n");
	PrintDebugInfo("----------\n");

	// Initialize COM objects
	CoInitializeEx(NULL,COINIT_MULTITHREADED);

	// Get the system ready
	D3DManager::getInstance()->Initialize(hInst,nullptr,"Muzzy A Wrappers",WinProc,WINDOW_WIDTH,WINDOW_HEIGHT,FULLSCREEN,MAXIMIZED);
	TextureManager::getInstance()->Initialize();
	DirectInput::getInstance()->Initialize(D3DManager::getInstance()->getHWND(),D3DManager::getInstance()->getHINSTANCE(),false);
	SoundManager::getInstance()->Initialize();

	// The Game
	Game::getInstance()->Initialize();
}

void Shutdown()
{
	PrintDebugInfo("\nShutdown\n");
	PrintDebugInfo("----------\n");

	// Let the system shut down
	Game::getInstance()->setRunning(false);
	Game::getInstance()->Shutdown();
	SoundManager::getInstance()->Shutdown();
	DirectInput::getInstance()->Shutdown();
	TextureManager::getInstance()->Shutdown();
	D3DManager::getInstance()->Shutdown();

	// Shutdown all COM Objects
	CoUninitialize();

	PrintDebugInfo(" Complete\n");
}

void PrintDebugInfo( const char *szString )
{
#ifdef _DEBUG
	cout << szString;
#endif
}
